package com.lh.common.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ProjectName: common
 * @Package: com.hebao.utils
 * @ClassName: HideDataUtil
 * @Description: java类作用描述
 * @Author: wudp
 * @CreateDate: 2018/10/30 上午11:06
 * @UpdateUser: 更新者
 * @UpdateDate: 2018/10/30 上午11:06
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HideDataUtil {
    private static String cardNo;

    /**
     * 方法描述 隐藏银行卡号中间的字符串（使用*号），显示前四后四
     *
     * @param cardNo
     * @return
     */
    public static String hideCardNo(String cardNo) {
        if(StringUtils.isBlank(cardNo)) {
            return cardNo;
        }
        int length = cardNo.length();
        int beforeLength = 4;
        int afterLength = 4;
        //替换字符串，当前使用“*”
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++) {
            if(i < beforeLength || i >= (length - afterLength)) {
                sb.append(cardNo.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }

        return sb.toString();
    }
    /**
     * 方法描述 隐藏手机号中间位置字符，显示前三后三个字符
     *
     * @param phoneNo
     * @return
     */
    public static String hidePhoneNo(String phoneNo) {
        if(StringUtils.isBlank(phoneNo)) {
            return phoneNo;
        }
        int length = phoneNo.length();
        int beforeLength = 3;
        int afterLength = 4;
        //替换字符串，当前使用“*”
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++) {
            if(i < beforeLength || i >= (length - afterLength)) {
                sb.append(phoneNo.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }
        return sb.toString();
    }

    /**
     * 正则姓名加密，保留最后一位，其余用*号代替
     * @param str
     * @return
     */
    public static String replaceNameX(String str){
        String reg = ".{1}";
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(str);
        int i = 0;
        while(m.find()){
            i++;
            if(i==str.length()) {
                continue;
            }
            m.appendReplacement(sb, "*");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 身份证
     * @param IDNumber
     * @return
     */
    public static String hideIDNumber(String IDNumber) {
        if (StringUtils.isBlank(IDNumber)) {
            return IDNumber;
        } else {
            int length = IDNumber.length();
            int beforeLength = 5;
            int afterLength = 3;
            String replaceSymbol = "*";
            StringBuffer sb = new StringBuffer();

            for(int i = 0; i < length; ++i) {
                if (i >= beforeLength && i < length - afterLength) {
                    sb.append(replaceSymbol);
                } else {
                    sb.append(IDNumber.charAt(i));
                }
            }

            return sb.toString();
        }
    }
}
